import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tplib/utils/image_utils.dart';
import 'package:tplib/utils/res_utils.dart';
import 'custom_floating_action_button_location.dart';
import 'simple_button.dart';

mixin CommWidgetManager{
  static KeyBoardOnTouchDownWidget(BuildContext context,{required Widget child}){
    return GestureDetector(
      onTap: ()=>FocusScope.of(context).requestFocus(FocusNode()),
      child: child,
    );
  }
  //普通页面脚手架
  static CommPageScaffoldWidget(BuildContext context,
      {
        Color? backgroundColor:ColorUtils.color_transparent,
        Color? statusFontColor:ColorUtils.color_white,
        PreferredSizeWidget? appBar,Widget? body,
        Widget? bottomNavigationBar,
        Widget? floatingActionButton,
        FloatingActionButtonLocation? floatingActionButtonLocation,
        bool? isShowBg:true,
        bool? isShowFloatingActionButton:false,
        Function? floatingActionButtonTap,
        double? pageWidth,
        double? pageHeight,
        String? appBarBgPath,
        String? iconCreateRoundPath,
      }){
    var designSize = MediaQuery.of(context).size;
    var paddingBottom = MediaQuery.of(context).padding.bottom;
    var paddingRight = MediaQuery.of(context).padding.right;
    LogUtil.e('CommPageScaffoldWidget-->paddingBottom=$paddingBottom,paddingRight=$paddingRight');
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: (statusFontColor==ColorUtils.color_white)?SystemUiOverlayStyle.light:SystemUiOverlayStyle.dark,
      child: Material(
        child:Stack(
          children: [
            (!(isShowBg??false))?Container():Positioned(
              top: PxUtils.px_0,
              left: PxUtils.px_0,
              child: Container(
                height: PxUtils.px_58+PxUtils.statusBarHeight,//固定高度为appbar的高度+状态栏的高度
                child: (appBarBgPath??'').isEmpty?
                    Container(
                      width: pageWidth??(designSize.width-paddingRight),
                      height: PxUtils.px_58+PxUtils.statusBarHeight,
                      color: ColorUtils.color_dcbe9b,
                    ):
                ImageUtils.loadAssetImage(
                  appBarBgPath,
                  width: pageWidth??(designSize.width-paddingRight),

                ),
              )
            ),
            Container(
              width: pageWidth??(designSize.width-paddingRight),
              height: pageHeight??(designSize.height-paddingBottom),
              child: KeyBoardOnTouchDownWidget(context, child: Scaffold(
                  backgroundColor:ColorUtils.color_transparent,
                  floatingActionButton:(!(isShowFloatingActionButton??false))?null:
                  (floatingActionButton??SimpleButton(
                    child:(iconCreateRoundPath??'').isEmpty?
                        Icon(Icons.add_circle_outline_rounded,size: PxUtils.px_100,):
                     ImageUtils.loadAssetImage(
                        iconCreateRoundPath,
                        width: PxUtils.px_100
                    ),
                    onPressed: (){
                      if(floatingActionButtonTap!=null)floatingActionButtonTap();
                    },
                  )),
                  //floatingActionButtonLocation的位置
                  floatingActionButtonLocation:(!(isShowFloatingActionButton??false))?null:
                  (floatingActionButtonLocation??CustomFloatingActionButtonLocation(
                      FloatingActionButtonLocation.endFloat,
                      0,
                      -PxUtils.px_50
                  )),
                  appBar: appBar,
                  bottomNavigationBar: bottomNavigationBar,
                  body: Container(
                    color: backgroundColor??ColorUtils.color_f2f2f2,
                    child: body,
                  )
              )),
            )
          ],
        )
        // Container(
        //   decoration: BoxDecoration(
        //     image: DecorationImage(
        //         fit: BoxFit.fitWidth,
        //         alignment: Alignment.topCenter,
        //         image: AssetImage(DrawableUtils.app_bar_bg)
        //     ),
        //   ),
        //
        //   child: KeyBoardOnTouchDownWidget(context, child: Scaffold(
        //       backgroundColor:ColorUtils.color_transparent,
        //       floatingActionButton:floatingActionButton,
        //       floatingActionButtonLocation: floatingActionButtonLocation,
        //       appBar: appBar,
        //       body: Container(
        //         color: backgroundColor??ColorUtils.color_f2f2f2,
        //         child: body,
        //       )
        //   )),
        // ),
      ),
    );
  }
  //圆形或者椭圆头像
  static buildCicleImagWidget(
      String? imgPath,
      {
        Key? key,
        String placeholder :"none",
        double? width,
        double? height,
        BoxFit fit: BoxFit.cover,
        EdgeInsetsGeometry? margin,
        EdgeInsetsGeometry? padding,
        Color? backgroundColor,
        bool isCircle:true,
        double roundRadius:5
      }){
    print('buildCicleWidget-->imgPath=$imgPath');
    return
      isCircle?
      Container(
        margin:margin,
        padding: padding,
        width: PxUtils.setSize(width??0),
        height: PxUtils.setSize(height??0),
        decoration: BoxDecoration(
            color: backgroundColor,
            borderRadius: BorderRadius.all(Radius.circular(width??0))
        ),
        child:(!(imgPath??'').startsWith('http'))?
        ClipOval(
          child: ImageUtils.loadAssetImage(
              placeholder,
              width: PxUtils.setSize(width??0),
              height: PxUtils.setSize(height??0),
              fit: BoxFit.fill
          ),
        ):
        ClipOval(
          child: ImageUtils.loadNetworkImage(
              imgPath,
              placeholder: placeholder,
              width: PxUtils.setSize(width??0),
              height: PxUtils.setSize(height??0),
              key: key,
              fit: BoxFit.fill
          ),
        ),
      ):Container(
        margin:margin,
        padding: padding,
        width: PxUtils.setSize(width??0),
        height: PxUtils.setSize(height??0),
        child:(!(imgPath??'').startsWith('http'))?
        ClipRRect(
          borderRadius: BorderRadius.circular(roundRadius),
          child: ImageUtils.loadAssetImage(
              placeholder,
              width: PxUtils.setSize(width??0),
              height: PxUtils.setSize(height??0),
              fit: BoxFit.fill
          ),
        ):
        ClipRRect(
          borderRadius: BorderRadius.circular(roundRadius),
          child: ImageUtils.loadNetworkImage(
              imgPath,
              placeholder: placeholder,
              width: PxUtils.setSize(width??0),
              height: PxUtils.setSize(height??0),
              key: key,
              fit: BoxFit.fill
          ),
        ),
      )
    ;
  }

  static buildCicleWidget(
      {
        Key? key,
        double? width,
        double? height,
        EdgeInsetsGeometry? margin,
        EdgeInsetsGeometry? padding,
        Color? backgroundColor,
        bool isCircle:true,
        double roundRadius:5,
        Widget? child
      }){
    return
      isCircle?
      Container(
        margin:margin,
        padding: padding,
        width: PxUtils.setSize(width??0),
        height: PxUtils.setSize(height??0),
        decoration: BoxDecoration(
            color: backgroundColor,
            borderRadius: BorderRadius.all(Radius.circular(width??0))
        ),
        child:
        ClipOval(
          child: child,
        )
      ):Container(
        margin:margin,
        padding: padding,
        width: PxUtils.setSize(width??0),
        height: PxUtils.setSize(height??0),
        child:
        ClipRRect(
          borderRadius: BorderRadius.circular(roundRadius),
          child: child,
        )
      )
    ;
  }
  static Widget commBar({
    double? width,
    double? height,
    Color? color,
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? padding,
    double? radius,
    bool isDash:false,
    bool isHor:true
  }){
    return
    Container(
      width: width??PxUtils.screenWidth,
      height: height??PxUtils.px_1,
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
          color: color??ColorUtils.color_f2f2f2,
          borderRadius: BorderRadius.circular(radius??PxUtils.px_0)
      ),
    );
  }
}

